package com.hzit.manager.controller;

import com.hzit.manager.service.IItemService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.system.api.domain.Item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * sku商品Controller
 *
 * @author hbxfwf
 * @date 2024-03-07
 */
@RestController
@RequestMapping("/item")
public class ItemController extends BaseController
{
    @Autowired
    private IItemService itemService;

    /**
     * 查询sku商品列表
     */
        @GetMapping("/list")
    public TableDataInfo list(Item item)
    {
        startPage();
        List<Item> list = itemService.selectItemList(item);
        return getDataTable(list);
    }

    // 查询sku商品信息
    @GetMapping("/findAll")
    public R<List<Item>> findAll(){
        Item item = new Item();
        item.setStatus("1");            // 审核通过
        List<Item> itemList = itemService.selectItemList(item);
        return R.ok(itemList);
    }
    // 根据goodsid查询sku商品列表
    @GetMapping("/findSkuList/{goodsId}")
    public R<List<Item>> findSkuList(@PathVariable("goodsId") Long goodsId){
        Item item = new Item();
        item.setGoodsId(goodsId);
        List<Item> itemList = itemService.selectItemList(item);
        return R.ok(itemList);
    }
    // 根据sku商品id查询sku商品
    @GetMapping("/findById/{id}")
    public R<Item> findById(@PathVariable("id") Long id){
        return R.ok(itemService.selectItemById(id));
    }
    /**
     * 导出sku商品列表
     */
        @Log(title = "sku商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Item item)
    {
        List<Item> list = itemService.selectItemList(item);
        ExcelUtil<Item> util = new ExcelUtil<Item>(Item.class);
        util.exportExcel(response, list, "sku商品数据");
    }

    /**
     * 获取sku商品详细信息
     */
        @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(itemService.selectItemById(id));
    }

    /**
     * 新增sku商品
     */
        @Log(title = "sku商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Item item)
    {
        return toAjax(itemService.insertItem(item));
    }

    /**
     * 修改sku商品
     */
        @Log(title = "sku商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Item item)
    {
        return toAjax(itemService.updateItem(item));
    }

    /**
     * 删除sku商品
     */
        @Log(title = "sku商品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(itemService.deleteItemByIds(ids));
    }
}
